<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/28
 * Time: 10:10
 */

namespace App\Game\Traits;


use Carbon\Carbon;

trait PtTrait
{
    public function apiDataParse(array $apiData, array $data)
    {
        $data = $this->gameCode($data);
        return $data;
    }

    private function gameCode(array $data)
    {
        if (array_key_exists('game_code', $data)) {
            $data['game_code'] = str_before(str_after($data['game_code'], '('), ')');
            $data['game_code']=str_before($data['game_code'],'_');
            $data['game_code']=preg_replace('/^([^\d]+).*/', '$1', $data['game_code']);
        }
        return $data;
    }

    /**
     * gameapi
     * 2018/9/21 9:43
     * Administrator
     * parseTimeInterval
     * 将时间拆分成最长半小时小时的时间断
     * @param Carbon $startTime
     * @param Carbon $endTime
     * @return array
     */
    public function parseTimeInterval(Carbon $startTime, Carbon $endTime)
    {
        $timeArray = [];
        $startMinute = (clone $startTime)->startOfMinute();
        $diffMinute = $startMinute->diffInMinutes($endTime, false);
        $diffHalfHours = ceil($diffMinute / 30);
        if ($startTime->diffInMinutes($endTime, false) > 30) {
            for ($i = 0; $i < $diffHalfHours; $i++) {
                if ($i < $diffHalfHours - 1) {
                    $start = (clone $startTime)->addMinute($i * 30)->format('Y-m-d H:i:s');
                    $end = (clone $startTime)->addMinute(($i + 1) * 30)->format('Y-m-d H:i:s');
                } else {
                    $start = (clone $startTime)->addMinute($i * 30)->format('Y-m-d H:i:s');
                    $end = (clone $endTime)->format('Y-m-d H:i:s');
                }

                $timeArray[] = compact('start', 'end');
            }
        } else {
            $start = (clone $startTime)->format('Y-m-d H:i:s');
            $end = (clone $endTime)->format('Y-m-d H:i:s');
            $timeArray[] = compact('start', 'end');
        }

        return $timeArray;
    }
}